﻿/******************************************************************************/
#include "stdafx.h"
#include "Main.h"
/******************************************************************************/
// MANAGE
/******************************************************************************/
Inventory::~Inventory()
{
   if(InvGui.inv==this)InvGui.link(NULL);
}
/******************************************************************************/
void Inventory::create(NetInventory &src)
{
   del();
   items.setNum(src.items.elms()); FREPA(src.items)item(i).create(src.items[i]);
   Copy(slots, src.slots);
   setGui();
}
void Inventory::setGui()
{
   if(InvGui.inv==this)InvGui.setGui();
}
/******************************************************************************/
// GET
/******************************************************************************/
Bool Inventory::slotCanBePutTo(SLOT_TYPE src, SLOT_TYPE dest)
{
   if(!InRange(src, SLOT_NUM) || !InRange(dest, SLOT_NUM))return false;

   return valid(src) ? item(slots[src]).canBePutTo(dest) : true;
}
/******************************************************************************/
Bool Inventory::slotsCanBeSwapped(SLOT_TYPE a, SLOT_TYPE b)
{
   return slotCanBePutTo(a, b) && slotCanBePutTo(b, a);
}
/******************************************************************************/
// UPDATE
/******************************************************************************/
void Inventory::update(CSkeleton &cskel, Chr *owner)
{
   if(owner && InvGui.inv==this)if(Player *player=CAST(Player, owner))InvGui.update(*player);

   // set matrixes for items in hands
   if(valid(SLOT_ARM_L))
   {
      if(C OrientP *point=cskel.findPoint("HandL"))
      {
         Item   &item  =T.item(slots[SLOT_ARM_L]);
         Matrix  matrix=item.matrix(); // get current matrix

         item.matrix(Matrix().setPosDir(point->pos, point->cross(), point->dir)); // set new matrix
         item.update();

         // immediately overwrite item members after calling 'update'
         GetVel(item.vel, item.ang_vel, matrix, item.matrix()); // calculate velocities between old and new matrixes
      }
   }

   if(valid(SLOT_ARM_R))
   {
      if(C OrientP *point=cskel.findPoint("HandR"))
      {
         Item   &item  =T.item(slots[SLOT_ARM_R]);
         Matrix  matrix=item.matrix(); // get current matrix

         item.matrix(Matrix().setPosDir(point->pos, point->cross(), point->dir)); // set new matrix
         item.update();

         // immediately overwrite item members after calling 'update'
         GetVel(item.vel, item.ang_vel, matrix, item.matrix()); // calculate velocities between old and new matrixes
      }
   }
}
/******************************************************************************/
// DRAW
/******************************************************************************/
UInt Inventory::drawPrepare()
{
   // draw items in hands
   UInt modes=0;
   if(valid(SLOT_ARM_L))modes|=item(slots[SLOT_ARM_L]).drawPrepare();
   if(valid(SLOT_ARM_R))modes|=item(slots[SLOT_ARM_R]).drawPrepare();
   return modes;
}
void Inventory::drawShadow()
{
   // draw items in hands
   if(valid(SLOT_ARM_L))item(slots[SLOT_ARM_L]).drawShadow();
   if(valid(SLOT_ARM_R))item(slots[SLOT_ARM_R]).drawShadow();
}
/******************************************************************************/
